import pyecharts.options as opts
from pyecharts.charts import Kline
from pyecharts.globals import ThemeType
from pyecharts.charts import Line
import akshare as ak

# 指数历史行情数据 - 新浪
df = ak.stock_zh_index_daily(symbol="sh000827")
print(df)
# k线数据
kline_data = []
for index, row in df.iterrows():
  kline_data.append([row['open'], row['close'], row['low'], row['high']])

# 配置 Kline 图
kline = (
  Kline()
  .add_xaxis(xaxis_data=df['date'].to_list())
  .add_yaxis(series_name="日K线", y_axis=kline_data)
  .set_global_opts(
    xaxis_opts=opts.AxisOpts(is_scale=True),
    yaxis_opts=opts.AxisOpts(is_scale=True),
    title_opts=opts.TitleOpts(title="中证环保指数k线"),
    # datazoom_opts=[opts.DataZoomOpts()],
    toolbox_opts=opts.ToolboxOpts(
      feature={
        "dataZoom": {"yAxisIndex": "none"},
        "restore": {},
        "saveAsImage": {},
      }
    ),
  )
)

# 渲染图表
kline.render("kline_chart.html")

# 指数估值
symbol = "沪深300"
df = ak.index_value_hist_funddb(symbol=symbol, indicator="市盈率")
df2 = ak.index_value_hist_funddb(symbol=symbol, indicator="市净率")

chart = Line(
    # 设置主题、画布宽度、高度等信息
    init_opts=opts.InitOpts(theme=ThemeType.DARK, width="1200px", height="600px"),
    # 是否内嵌js 默认false
    # render_opts=opts.RenderOpts(is_embed_js=True)
)

chart.set_global_opts(
    title_opts=opts.TitleOpts(title="中证环保"),
    # 工具箱配置
    toolbox_opts=opts.ToolboxOpts(is_show=True),
    # 区域缩放配置
    datazoom_opts=[opts.DataZoomOpts(is_show=True)],
)

x_data = df["日期"].tolist()
y_data = df["市盈率"].tolist()
y_data2 = df2["市净率"].tolist()

chart.add_xaxis(x_data)
chart.add_yaxis(
"市盈率",
            y_data,
            markpoint_opts=opts.MarkPointOpts(
                data=[opts.MarkPointItem(type_="max"), opts.MarkPointItem(type_="min")],
            ),
            # 标记线
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(type_="average")]
            ),
)
chart.add_yaxis("市净率", y_data2, is_smooth=True)

chart.render("kline_chart.html")